package com.atguigu.dao.impl;

import com.atguigu.dao.OrderDao;
import com.atguigu.pojo.Order;

import java.util.List;

public class OrderDaoImpl extends BaseDao implements OrderDao {
    /**
     * 保存订单信息
     * @param order
     * @return
     */
    @Override
    public int saveOrder(Order order) {
        String sql = "insert into t_order(`order_id`,`create_time`,`price`,`status`,`user_id`) values(?,?,?,?,?)";
        return update(sql, order.getOrder_id(),order.getCreate_time(),order.getPrice(),order.getStatus(),order.getUser_id());
    }

    /**
     * 查询所有订单
     * @return
     */
    @Override
    public List<Order> queryOrders() {
        String sql ="select `order_id`,`create_time`,`price`,`status`from t_order";
        return queryForList(Order.class, sql);
    }

    /**
     * 改变订单的状态
     * @param orderId
     * @param status
     * @return
     */
    @Override
    public int changeOrderStatus(String orderId, Integer status) {
        String sql = "update t_order set status=? where order_id=?";
        return  update(sql, status,orderId);
    }

    /**
     * 根据用户编号查询订单信息
     * @param userId
     * @return
     */
    @Override
    public Order queryOrdersByUserId(Integer userId) {
        String sql = "select `order_id`,`create_time`,`price`,`status`from t_order where user_id=?";
        return queryForOne(Order.class, sql, userId);
    }


}
